package r25.tools;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.EnumMap;
import java.util.Locale;
import java.util.Map;

/**
 * <p>DateHelper class.</p>
 *
 * @author u211354
 * @version $Id: $
 */
public class DateHelper {
    private static final Calendar cal = Calendar.getInstance(Locale.FRENCH);
    private static final Map<DF,DateFormat> dfs = new EnumMap<DF,DateFormat>(
            DF.class);

    public enum DF {
        INTERNAL,
        INTERNET,
        LUCENE,
        SIGNED_BEAN,
        RAW,
        ISO_8601;
    }

    static {
        setDateFormat(DF.INTERNAL, "yyyy-MM-dd");
        setDateFormat(DF.RAW, "EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH,
                false);
        setDateFormat(DF.INTERNET, "yyyy-MM-dd'T'HH:mm:ss'Z'");
        setDateFormat(DF.LUCENE, "yyyy-MM-dd'T'HH:mm:ss'Z'");
        setDateFormat(DF.SIGNED_BEAN, "yyyy-MM-dd.HHmmss");
        setDateFormat(DF.ISO_8601, "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    }

    private static void setDateFormat(DF key, String format) {
        setDateFormat(key, format, Locale.FRENCH, false);
    }

    private static void setDateFormat(DF key, String format, Locale locale,
            boolean lenient) {
        DateFormat df = new SimpleDateFormat(format, locale);
        df.setLenient(lenient);
        dfs.put(key, df);
    }

    private DateHelper() {
    }

    /**
     * <p>getDateFormat</p>
     *
     * @return a {@link java.text.DateFormat} object.
     */
    public static DateFormat getDateFormat() {
        return (DateFormat)dfs.get(DF.INTERNAL).clone();
    }

    /**
     * <p>getDateFormat</p>
     *
     * @param key a {@link wonder.tools.DateHelper.DF} object.
     * @return a {@link java.text.DateFormat} object.
     */
    public static DateFormat getDateFormat(DF key) {
        return (DateFormat)dfs.get(key).clone();
    }

    /**
     * <p>previousDay</p>
     *
     * @param day a {@link java.util.Date} object.
     * @return a {@link java.util.Date} object.
     */
    public synchronized static Date previousDay(Date day) {
        cal.setTime(day);
        cal.add(Calendar.DATE, -1);
        return cal.getTime();
    }

    /**
     * <p>nextDay</p>
     *
     * @param day a {@link java.util.Date} object.
     * @return a {@link java.util.Date} object.
     */
    public synchronized static Date nextDay(Date day) {
        cal.setTime(day);
        cal.add(Calendar.DATE, 1);
        return cal.getTime();
    }

}
